var accout={
	payNum:null,
	payPwd:null,
	sysPayNum:null,
	sysPayPwd:null,
	totalMoney : null,
	init:function(){
		var data=window.sessionStorage.getItem('accoutD');
		if(data){
			data=JSON.parse(data).skuid;
			app.ajax(
	    		'/Goods/Cart/upgradehongren',
	    		{
	    			skuid : data
	    		},
	    		function(res){
	    			if(res.code ==200){
	    				if(window.sessionStorage.getItem('choice_Addr')){
							mess=window.sessionStorage.getItem('choice_Addr');
							console.log(JSON.parse(mess));
							mess = JSON.parse(mess);
							console.log(mess);
							res.address_list.id = mess.id;
							res.address_list.name = mess.uname;
							res.address_list.mobile = mess.tel;
							res.address_list.area = mess.area;
						}
	    				console.log(res.address_list);
				    	app.tpl($('.account'),'tpl-account',res);
	    				window.sessionStorage.setItem('totalCount',res.total_money);	
	    			}else{
	    				layer.msg('系统繁忙，请稍候再试');
	    			}
	    		}
	    	);
	    	window.sessionStorage.setItem('accoutD','');

		}else{
			app.ajax(
	    		'/Goods/Cart/cartSettlement',
	    		null,
	    		function(res){
	    			if(res.code ==200){
	    				if(window.sessionStorage.getItem('choice_Addr')){
							mess=window.sessionStorage.getItem('choice_Addr');
							console.log(JSON.parse(mess));
							mess = JSON.parse(mess);
							console.log(mess);
							res.address_list.id = mess.id;
							res.address_list.name = mess.uname;
							res.address_list.mobile = mess.tel;
							res.address_list.area = mess.area;
						}
	    				app.tpl($('.account'),'tpl-account',res);
	    				window.sessionStorage.setItem('totalCount',res.total_money);
	    			}else{
	    				layer.msg('系统繁忙，请稍候再试');
	    			}
	    		}
	    	);
    	}
    	this.payNum = $('#pay-num');
    	this.payPwd = $('#pwd');
    	this.totalMoney = $('.price .num');
    	var price=$('.price .num').text();
		window.sessionStorage.setItem('totalMoney',price);
	},
	validBalNum : function(num){
		this.sysPayNum = parseFloat($('#pay-num').attr('data-money'));
		var totalMoney = parseFloat($('.price .num').text());
		if(num > this.sysPayNum){
			console.log(2);
			layer.msg('您的余额只有'+this.sysPayNum);
			$('#pay-num').val(this.sysPayNum);
		}else if(num > totalMoney){
			layer.msg('输入金额不能超过订单金额');
			this.payNum.val(totalMoney);
		}
		var num = parseFloat($('#pay-num').val());
		
		var total = window.sessionStorage.getItem('totalCount');
		total = parseFloat(total);
		console.log('num'+num+','+total);
		$('.price .num').text(total - num);
		
		
	},
	validBalPwd : function(pwd){
		this.sysPayPwd = $('#pwd').attr('data-pwd');
		if(pwd = this.sysPayPwd){
			layer.msg('密码错误！请重新输入');
			$('#pwd').val('');
			$('#pwd').focus();
		}else{
			var money1 = parseFloat($('.price .num').text()),
			money2 = parseFloat(this.payNum.val());
			$('.price .num').text((money1- money2).toFixed(2));
		}
	},
	balance:function(data){
		app.ajax(
			'/Goods/Order/order_add',
			data,
			function(res){
				console.log(JSON.stringify(res.html));
				if(res.code == 200){
					$(".topay").removeAttr("disabled")
					$('body').html(res.html);
				}else{
					layer.msg(res.msg);
				}
			}
		);
	},
	weChat:function(data){
		app.ajax(
			'',
			data,
			function(res){
				if(res == 200){
					
				}
			}
		);
	}

}

window.onload=function(){
	accout.init();
	header.init('提交订单');	
	footer.init();
	mui.init({
		swipeBack:false //启用右滑关闭功能
	});
	

	var ua = window.navigator.userAgent.toLowerCase();
	setTimeout(function(){
		if(ua.match(/MicroMessenger/i) != 'micromessenger'){
			$('.weChat').hide();
			$('.alipay').removeClass('dB');
			$('.alipay img.choice-css').attr('src','../img/icon/choice2.png');
		}else{
			$('.alipay').hide();
			$('.weChat img.choice-css').attr('src','../img/icon/choice2.png');
		}
	},1000);

	setTimeout(function(){
	var data=window.sessionStorage.getItem('acconttovouchers'),
		thenid='',
		then='',
		data = JSON.parse(data);
		if(data){
			thenid = data.id;
			then = data.then;
			$('.use-vouchers p.cGrey').text(then);
			$('.user-vouchers p.cGrey').attr('data-id',thenid);
		}
	},2000);
	/*验证输入金额**/
	$('body').on('blur','#pay-num',function(){
		if(!$(this).val()){
			layer.msg('输入不能为空');
			$(this).focus();
			return;
		}
		var num = parseFloat($(this).val());
		accout.validBalNum(num);
	});
	
	$('body').on('blur','#pwd',function(){
		if(!$(this).val()){
			layer.msg('输入不能为空');
			$(this).focus();
			return;
		}
		var num = parseFloat($(this).val());
		accout.validBalPwd(num);
	});
	
	$('body').on('click','.use-vouchers',function(){
		app.open('accontToVouchers.html','accontToVouchers',{},window.location.href,document.body.scrollTop);
	});
	
	$('body').on('click','.addr',function(){
		app.open('user.choiceAddr.html','choiceAddr',{},window.location.href,document.body.scrollTop);
	});
	$('body').on('click','#switch',function(e){
			if(!$(this).attr('data-money')){
				lay.msg('您的余额不足');
			}			
			var pwd = $('.choice-pay #pwd').attr('data-pwd');
			if(!pwd){
				$('.choice-pay .btn-white').show();
				$('.choice-pay #pwd').hide();
			}
			if (!$(this).hasClass('mui-active')) {
				$(this).attr('style','transition-durantion:0.2s');
				$(this).find('mui-switch-handle').attr('style','transition-duration: 0.2s; transform: translate(43px, 0px)');
				$(this).addClass('mui-active');
				$('.choice-pay').show();
			} else {
				$('.choice-pay').hide();
				$(this).removeClass('mui-active');
			}
	});
	
	/**按钮切换效果*/
	$('body').on('click','.pay-choice',function(){
		if($(this).find('img').attr('src').indexOf('choice1') != -1){
			$('.pay-choice').find('img').attr('src','../img/icon/choice1.png');
			$(this).find('img').attr('src','../img/icon/choice2.png');
		}else{
			$('.pay-choice').find('img').attr('src','../img/icon/choice1.png');
		}
	})
	/**编辑地址*/
	
	/**使用代金劵*
	 * 
	 */
	/*结算按钮**/
	$('body').on('click','.topay',function(){
		$(this).attr({"disabled":"disabled"});
		var addr=$('.receive-addr').attr('data-addrId'),
		type=1,
		isUseCash = 0,
		pwd = $('.choice-pay #pwd').val(),
		price = $('#pay-num').val(),
		cpns_id = 0,
		canshu = {};
		if(!pwd){
			pwd = '';
		}
		if(!price){
			price = 0;
		}
		if($('#switch').hasClass('mui-active')){
			isUseCash = 1;
		}
		if(window.sessionStorage.getItem('acconttovouchers')){
		var data=window.sessionStorage.getItem('acconttovouchers'),
		thenid='',
		then='',
		data = JSON.parse(data);
		cpns_id = data.id;
		}
		canshu = {
			addressId : addr,
			isUseCash :isUseCash,
			money : price,
			payPwd    : pwd,
			cpns_id : cpns_id,
			payTypeId : type
		}
		var data=window.sessionStorage.getItem('accoutD');
		if(data){
			data = JSON.parse(data);
			canshu.supplier_id = data.data1.supplierId;
			canshu.warehouse_type = data.data1.warehouse_type;
			canshu.skuIds = data.data1.skuid;
		}
		if($('.alipay .fr img').attr('src').indexOf('choice2') != -1){
			type = 1;
			accout.balance(canshu);
		}else if($('.weChat .fr img').attr('src').indexOf('choice2') != -1){
			type = 2;
			accout.wechat(canshu);
		}
	});
};
